<template>
    <div class="bankingInformationPage">
        <div class="search-wrap d-flex a-center">

            <div class="search-item d-flex a-center m-l20">
                <label>机构名称</label>
                <el-select class="m-l10" v-model="form.companyId" clearable>
                    <el-option v-for="(item,index) in companyList" :key="index" :label="item.companyName" :value="item.companyId"></el-option>
                </el-select>
            </div>
            <div class="search-item d-flex a-center m-l20">
                <label>开户行</label>
                <el-select class="m-l10" v-model="form.bankAccountId" clearable>
                    <el-option v-for="(item,index) in bankAccountList" :key="index" :label="item.bankName" :value="item.bankAccountId"></el-option>
                </el-select>
            </div>
            <div class="search-item d-flex a-center m-l20">
                <label>账户类型</label>
                <el-select class="m-l10" v-model="form.accountType" clearable>
                    <el-option label="基本户" value="1"></el-option>
                    <el-option label="一般户" value="2"></el-option>
                </el-select>
            </div>
            <div class="search-buttons m-l50 d-flex a-center">
                <el-button
                        color="#fc702b"
                        style="color:#fff;"
                        @click.prevent="onQuery"
                >查询</el-button>
                <el-button
                        color="#f0f0f0"
                        style="color: #000000"
                        @click.prevent="onReset"
                >重置</el-button>
                <el-divider direction="vertical" />
                <el-button
                        @click="onImport"
                        class="button_edit m-r10"
                >
                    上传余额表
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onDownloadTemplate"
                >
                    下载余额表模版
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onExport"
                >
                    <svg-icon icon-class="project-detail-icon-derive" class="icon-export m-r5"></svg-icon>
                    一键导出
                </el-button>
            </div>
        </div>
        <public-table
                ref="publicTableRef"
                class="m-t20"
                index
                :indexWidth="'100'"
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
        ></public-table>
    </div>

  <!-- 导入excel   -->
    <public-dialog
            :title="'导入余额表'"
            :width="'40%'"
            :confirmButtonTitle="'确认'"
            v-model:visible="visible"
            @onConfirm="onConfirm"
    >
        <div class="public-dialog-content d-flex flex-column">
            <el-upload
                    ref="uploadRef"
                    class="upload-demo"
                    drag
                    action="#"
                    multiple
                    :http-request="uploadFile"
            >
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="el-upload__text">
                    将文件拖到此处或<em>点击上传</em>
                </div>
            </el-upload>
        </div>
    </public-dialog>
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {
    ElSelect,
    ElOption,
    ElDivider,
    ElUpload, ElLoading, ElMessage
} from "element-plus";
import PublicTable from "@/components/PublicTable.vue";

import {tableCommonFun} from "@/hooks/tableCommon";
import {companyApi} from "@/api/businessFinance";
import {exportExcelFile} from "@/common/utils/GlobalUtils";
import PublicDialog from "@/components/PublicDialog.vue";
import {UploadFilled} from "@element-plus/icons-vue";

const interfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'companyApi.getBankBalance',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'exportApi.getBankBalanceLeadingOut',
    tableName:'银行余额表'
});
const {
    form,
    page,
    tableOption,
    tableData,
    onQuery,
    onReset,
    onPageChange,
    getData,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    companyId: '',
    bankAccountId: '',
    accountType: ''
}

tableOption.value = [
  { label: "机构名称 ", prop: "companyName", minWidth: 150 },
  { label: "开户行", prop: "bankName", minWidth: 120 },
  {
    label: "账号类型", prop: "accountType", formatter: (val, row) => {
      return ['', '基本户', '一般户'][val] || '-';
    }, minWidth: 100
  },
  { label: "账户用途", prop: "accountPurpose", minWidth: 100 },
  { label: "银行账户号", prop: "bankAccountNumber", minWidth: 150 },
  { label: "上周余额", prop: "lastWeekBalance", minWidth: 100 },
  { label: "本周变动情况", prop: "changesThisWeek", minWidth: 100 },
  { label: "余额", prop: "balance", minWidth: 100 },
];


const companyList = ref([]);
/**
 * 查公司列表
 */
const getCompanyList = () => {
    companyApi.getList({}).then(res=>{
        companyList.value = res.data;
    });
}

/**
 * 查询银行列表
 *
 */
const bankAccountList = ref([]);
const getBankList = () => {
    companyApi.getBankList().then(res=>{
        if(res.code === '200'){
            bankAccountList.value = res.data;
        }
    });
}

/**
 * 下载余额模版
 *
 */
const onDownloadTemplate = () => {
    companyApi.downloadTemplate().then(res=>{
        if(res){
            exportExcelFile(res,'余额表导入模板');
        }
    });
}

const visible = ref(false);
const onConfirm = () => {

}

const onImport = () => {
    visible.value = true;
}

const uploadFile = (file) => {
    const loading = ElLoading.service({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
    });
    let uploadFile = new FormData();
    uploadFile.append("file", file.file);
    companyApi.importExcel(uploadFile).then(res=>{
        if (res.code === '200') {
            loading.close();
            ElMessage.success("上传成功!");
            visible.value = false;
            getData({
                pageNum:1,
                pageSize:10
            });
        }else{
            loading.close();
            ElMessage.error("上传失败!请联系管理员。");
        }
    });
}

onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:10
    });
    getCompanyList();
    getBankList();
});
</script>

<style lang="scss" scoped>
.bankingInformationPage{
  .search-wrap{
    .search-item{
      .el-input{
        width: auto !important;
      }
    }
    .search-buttons{
      .icon-export{
        width: 18px;
        height: 18px;
      }
    }
  }
}
.public-dialog-content{
  .dialog-title-box{
    height: 30px;
    span{
      font-size: 15px;
      font-weight: 600;
    }
  }
}
</style>
